package adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.lxh.testinfo.AChartEngineActivity;
import com.example.lxh.testinfo.R;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.BarChart;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.CategorySeries;
import org.achartengine.model.SeriesSelection;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.SimpleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import database.sqliteUtil;
import jsonbean.Quliaty;

/**
 * Created by Lxh on 2017/12/27.
 */

public class MyViewpageAdapter extends PagerAdapter {
    private List<View> list;
    private LinearLayout m_chart_hist;
    public GraphicalView graphicalView;
    public List<Double> listco;
    public List<Double> listtemp;
    public List<Double> listpm;
    public List<Double> listhum;


    public MyViewpageAdapter(List<View> list, Context context) {
        this.list = list;
        sqliteUtil util = new sqliteUtil(context);
        List<Quliaty> lists = util.query();
        listco = new ArrayList<>();
        listtemp = new ArrayList<>();
        listpm = new ArrayList<>();
        listhum = new ArrayList<>();
        Log.w("Lxh", "查询出来的数据 ： " + lists.size());
        for (Quliaty quliaty : lists) {
            listco.add((double) quliaty.getCo2());
            listhum.add((double) quliaty.getHumidity());
            listpm.add((double) quliaty.getPm());
            listtemp.add((double) quliaty.getTemp());
        }
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(list.get(position));
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        container.addView(list.get(position));
        View views = list.get(position);
        m_chart_hist = views.findViewById(R.id.chart_hist);
        switch (position) {
            case 0:
                XYMultipleSeriesDataset mDaraset1 = getDataSet_hist();
                XYMultipleSeriesRenderer mRefender1 = getRefeder_hist();
                graphicalView = ChartFactory.getBarChartView(views.getContext(), mDaraset1, mRefender1, BarChart.Type.DEFAULT);
                m_chart_hist.addView(graphicalView);
                break;
            case 1:
                XYMultipleSeriesDataset mDaraset = getDataSet_tmp1();
                XYMultipleSeriesRenderer mRefender = getRefeder1();
                graphicalView = ChartFactory.getLineChartView(views.getContext(), mDaraset, mRefender);
                m_chart_hist.addView(graphicalView);
                break;
            case 2:
                XYMultipleSeriesDataset mDaraset2 = getDataSet_tmp2();
                XYMultipleSeriesRenderer mRefender2 = getRefeder2();
                graphicalView = ChartFactory.getLineChartView(views.getContext(), mDaraset2, mRefender2);
                m_chart_hist.addView(graphicalView);
                break;
            case 3:
                XYMultipleSeriesDataset mDaraset3 = getDataSet_tmp3();
                XYMultipleSeriesRenderer mRefender3 = getRefeder3();
                graphicalView = ChartFactory.getLineChartView(views.getContext(), mDaraset3, mRefender3);
                m_chart_hist.addView(graphicalView);
                break;
        }
        return list.get(position);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    public XYMultipleSeriesDataset getDataSet_hist() {
        XYMultipleSeriesDataset dataSet_hist = new XYMultipleSeriesDataset();
        CategorySeries series = new CategorySeries("Demo");
        for (int i = 0; i < listpm.size(); i++) {
            series.add(listpm.get(i));
        }
        dataSet_hist.addSeries(series.toXYSeries());
        return dataSet_hist;
    }

    public XYMultipleSeriesRenderer getRefeder_hist() {
        XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
        SimpleSeriesRenderer r = new SimpleSeriesRenderer();
        r.setColor(Color.parseColor("#979797"));
        renderer.addSeriesRenderer(r);

        renderer.setZoomButtonsVisible(false);
        renderer.setZoomEnabled(false);
        renderer.setZoomInLimitX(7);
        renderer.setPanEnabled(false, false); //设置 左右滑动 ， 不许上下滑动
        renderer.setShowAxes(false);// 是否显示 坐标轴
        renderer.setYLabelsColor(0, Color.BLACK); //Y轴标签的颜色

        renderer.setLabelsTextSize(18);  //设置标签的字体大小
        renderer.setApplyBackgroundColor(true);//设置是否显示背景颜色
        renderer.setBackgroundColor(Color.WHITE);//设置背景颜色
        renderer.setMarginsColor(Color.WHITE);//设置图标周围的颜色
        renderer.setClickEnabled(true);//设置是否可点击


        renderer.setShowGridX(true); //是否显示网格  X
        renderer.setGridColor(Color.parseColor("#979797")); //设置网格颜色
        renderer.setBarSpacing(1.0); //柱子的粗细
        renderer.setYLabels(6);  //  X  Y要显示的标签
        renderer.setXLabels(0);
        renderer.addXTextLabel(1, "03");
        renderer.addXTextLabel(2, "06");
        renderer.addXTextLabel(3, "09");
        renderer.addXTextLabel(4, "12");
        renderer.addXTextLabel(5, "15");
        renderer.addXTextLabel(6, "18");
        renderer.addXTextLabel(7, "21");
        renderer.addXTextLabel(8, "24");
        renderer.addXTextLabel(9, "27");
        renderer.addXTextLabel(10, "30");

        renderer.addXTextLabel(11, "33");
        renderer.addXTextLabel(12, "36");
        renderer.addXTextLabel(13, "39");
        renderer.addXTextLabel(14, "42");
        renderer.addXTextLabel(15, "45");
        renderer.addXTextLabel(16, "48");
        renderer.addXTextLabel(17, "51");
        renderer.addXTextLabel(18, "54");
        renderer.addXTextLabel(19, "57");
        renderer.addXTextLabel(20, "60");

        renderer.setXLabelsColor(Color.BLACK);
        renderer.setMargins(new int[]{0, 20, 0, 20}); //设置外边距   上左下右
        renderer.setXTitle("x轴");
        renderer.setYTitle("Y轴");
        renderer.setXAxisMin(0.5);
        renderer.setXAxisMax(25.0);
        renderer.setYAxisMin(0);
        renderer.setYAxisMax(210);
        return renderer;
    }

    private XYMultipleSeriesRenderer getRefeder1() {
        XYMultipleSeriesRenderer seriesRenderer = new XYMultipleSeriesRenderer();
        seriesRenderer.setMargins(new int[]{0, 20, 0, 20});//设置外边距   上左下右
        seriesRenderer.setAxisTitleTextSize(20);//设置坐标轴标题的大小
        seriesRenderer.setYAxisMin(0); //y轴的起始值
        seriesRenderer.setYAxisMax(40);  //最大值
        seriesRenderer.setXAxisMin(0.5); //x轴的起始值
        seriesRenderer.setXAxisMax(25.0);// x轴的最大值

        seriesRenderer.setShowGridX(true);
        seriesRenderer.setGridColor(Color.BLACK);
        seriesRenderer.setDisplayValues(false);
        seriesRenderer.setShowAxes(false);


        seriesRenderer.setApplyBackgroundColor(true);
        seriesRenderer.setLabelsColor(0x000000);
        seriesRenderer.setBackgroundColor(Color.WHITE);

        seriesRenderer.setZoomButtonsVisible(false);
        seriesRenderer.setZoomEnabled(false);
        seriesRenderer.setZoomInLimitX(7);
        seriesRenderer.setPanEnabled(false);
        seriesRenderer.setChartValuesTextSize(20);

//        seriesRenderer.setYLabelsColor(0, Color.BLACK);
//        seriesRenderer.setLabelsTextSize(18);
        seriesRenderer.setXLabelsAlign(Paint.Align.CENTER);
        seriesRenderer.setYLabelsAlign(Paint.Align.LEFT);
        seriesRenderer.setYLabels(6);//设置X Y轴显示的值
        seriesRenderer.setXLabels(0);
        seriesRenderer.addXTextLabel(1, "03");
        seriesRenderer.addXTextLabel(2, "06");
        seriesRenderer.addXTextLabel(3, "09");
        seriesRenderer.addXTextLabel(4, "12");
        seriesRenderer.addXTextLabel(5, "15");
        seriesRenderer.addXTextLabel(6, "18");
        seriesRenderer.addXTextLabel(7, "21");
        seriesRenderer.addXTextLabel(8, "24");
        seriesRenderer.addXTextLabel(9, "27");
        seriesRenderer.addXTextLabel(10, "30");
        seriesRenderer.addXTextLabel(11, "33");
        seriesRenderer.addXTextLabel(12, "36");
        seriesRenderer.addXTextLabel(13, "39");
        seriesRenderer.addXTextLabel(14, "42");
        seriesRenderer.addXTextLabel(15, "45");
        seriesRenderer.addXTextLabel(16, "48");
        seriesRenderer.addXTextLabel(17, "51");
        seriesRenderer.addXTextLabel(18, "54");
        seriesRenderer.addXTextLabel(19, "57");
        seriesRenderer.addXTextLabel(20, "60");

        seriesRenderer.setXLabelsColor(Color.BLACK);
        seriesRenderer.setLabelsTextSize(20);
        seriesRenderer.setYLabelsColor(0, Color.BLACK);
        seriesRenderer.setPointSize(12f);
        seriesRenderer.setMarginsColor(Color.WHITE);
        seriesRenderer.setClickEnabled(false);

        XYSeriesRenderer xySeriesRenderer1 = new XYSeriesRenderer();
        xySeriesRenderer1.setPointStyle(PointStyle.CIRCLE);
        xySeriesRenderer1.setPointStrokeWidth(3);
        xySeriesRenderer1.setColor(Color.GRAY);
        xySeriesRenderer1.setFillPoints(true);
        xySeriesRenderer1.setDisplayChartValues(false);
        xySeriesRenderer1.setChartValuesTextSize(14);
        xySeriesRenderer1.setLineWidth(4.0f);//设置 线的宽度
        seriesRenderer.addSeriesRenderer(xySeriesRenderer1);
        return seriesRenderer;
    }

    public XYMultipleSeriesDataset getDataSet_tmp1() {
        XYMultipleSeriesDataset seriesDataset = new XYMultipleSeriesDataset();
        XYSeries xySeries = new XYSeries("温度");
        for (int i = 0; i < listtemp.size(); i++) {
            xySeries.add(i + 1, listtemp.get(i));
        }
        seriesDataset.addSeries(xySeries);
        return seriesDataset;
    }

    private XYMultipleSeriesRenderer getRefeder2() {
        XYMultipleSeriesRenderer seriesRenderer = new XYMultipleSeriesRenderer();
        seriesRenderer.setMargins(new int[]{0, 20, 0, 20});//设置外边距   上左下右
        seriesRenderer.setAxisTitleTextSize(20);//设置坐标轴标题的大小
        seriesRenderer.setYAxisMin(20); //y轴的起始值
        seriesRenderer.setYAxisMax(90);  //最大值
        seriesRenderer.setShowGridX(true);
        seriesRenderer.setGridColor(Color.BLACK);
        seriesRenderer.setDisplayValues(false);
        seriesRenderer.setShowAxes(false);

        seriesRenderer.setXAxisMin(0.5); //x轴的起始值
        seriesRenderer.setXAxisMax(25.0);// x轴的最大值
        seriesRenderer.setApplyBackgroundColor(true);
        seriesRenderer.setLabelsColor(0x000000);
        seriesRenderer.setBackgroundColor(Color.WHITE);

        seriesRenderer.setZoomButtonsVisible(false);
        seriesRenderer.setZoomEnabled(false);
        seriesRenderer.setZoomInLimitX(7);
        seriesRenderer.setPanEnabled(false);

        seriesRenderer.setXLabelsAlign(Paint.Align.CENTER);
        seriesRenderer.setYLabelsAlign(Paint.Align.LEFT);
        seriesRenderer.setYLabels(6);//设置X Y轴显示的值
        seriesRenderer.setXLabels(0);
        seriesRenderer.addXTextLabel(1, "03");
        seriesRenderer.addXTextLabel(2, "06");
        seriesRenderer.addXTextLabel(3, "09");
        seriesRenderer.addXTextLabel(4, "12");
        seriesRenderer.addXTextLabel(5, "15");
        seriesRenderer.addXTextLabel(6, "18");
        seriesRenderer.addXTextLabel(7, "21");
        seriesRenderer.addXTextLabel(8, "24");
        seriesRenderer.addXTextLabel(9, "27");
        seriesRenderer.addXTextLabel(10, "30");
        seriesRenderer.addXTextLabel(11, "33");
        seriesRenderer.addXTextLabel(12, "36");
        seriesRenderer.addXTextLabel(13, "39");
        seriesRenderer.addXTextLabel(14, "42");
        seriesRenderer.addXTextLabel(15, "45");
        seriesRenderer.addXTextLabel(16, "48");
        seriesRenderer.addXTextLabel(17, "51");
        seriesRenderer.addXTextLabel(18, "54");
        seriesRenderer.addXTextLabel(19, "57");
        seriesRenderer.addXTextLabel(20, "60");
        seriesRenderer.setXLabelsColor(Color.BLACK);
        seriesRenderer.setLabelsTextSize(20);
        seriesRenderer.setYLabelsColor(0, Color.BLACK);
        seriesRenderer.setPointSize(12f);
        seriesRenderer.setMarginsColor(Color.WHITE);
        seriesRenderer.setClickEnabled(false);

        XYSeriesRenderer xySeriesRenderer1 = new XYSeriesRenderer();
        xySeriesRenderer1.setPointStyle(PointStyle.CIRCLE);
        xySeriesRenderer1.setPointStrokeWidth(3);
        xySeriesRenderer1.setColor(Color.GRAY);
        xySeriesRenderer1.setFillPoints(true);
        xySeriesRenderer1.setDisplayChartValues(false);
        xySeriesRenderer1.setChartValuesTextSize(14);
        xySeriesRenderer1.setLineWidth(4.0f);//设置 线的宽度
        seriesRenderer.addSeriesRenderer(xySeriesRenderer1);
        return seriesRenderer;
    }

    public XYMultipleSeriesDataset getDataSet_tmp2() {
        XYMultipleSeriesDataset seriesDataset = new XYMultipleSeriesDataset();
        XYSeries xySeries = new XYSeries("相对湿度");
        for (int i = 0; i < listhum.size(); i++) {
            xySeries.add(i + 1, listhum.get(i));
        }
        seriesDataset.addSeries(xySeries);
        return seriesDataset;
    }

    private XYMultipleSeriesRenderer getRefeder3() {
        XYMultipleSeriesRenderer seriesRenderer = new XYMultipleSeriesRenderer();
        seriesRenderer.setMargins(new int[]{0, 20, 0, 20});//设置外边距   上左下右
        seriesRenderer.setAxisTitleTextSize(20);//设置坐标轴标题的大小
        seriesRenderer.setYAxisMin(200); //y轴的起始值
        seriesRenderer.setYAxisMax(800);  //最大值
        seriesRenderer.setDisplayValues(false);
        seriesRenderer.setShowAxes(false);

        seriesRenderer.setXAxisMin(0.5); //x轴的起始值
        seriesRenderer.setXAxisMax(25.0);// x轴的最大值
        seriesRenderer.setApplyBackgroundColor(true);
        seriesRenderer.setLabelsColor(0xFFFFFF);
        seriesRenderer.setBackgroundColor(Color.parseColor("#52C6F7"));

        seriesRenderer.setZoomButtonsVisible(false);
        seriesRenderer.setZoomEnabled(false);
        seriesRenderer.setZoomInLimitX(7);
        seriesRenderer.setPanEnabled(false);
        seriesRenderer.setChartValuesTextSize(20);

//        seriesRenderer.setYLabelsColor(0, Color.BLACK);
//        seriesRenderer.setLabelsTextSize(18);
        seriesRenderer.setXLabelsAlign(Paint.Align.CENTER);
        seriesRenderer.setYLabelsAlign(Paint.Align.LEFT);
        seriesRenderer.setYLabels(4);//设置X Y轴显示的值
        seriesRenderer.setXLabels(0);
        seriesRenderer.addXTextLabel(1, "03");
        seriesRenderer.addXTextLabel(2, "06");
        seriesRenderer.addXTextLabel(3, "09");
        seriesRenderer.addXTextLabel(4, "12");
        seriesRenderer.addXTextLabel(5, "15");
        seriesRenderer.addXTextLabel(6, "18");
        seriesRenderer.addXTextLabel(7, "21");
        seriesRenderer.addXTextLabel(8, "24");
        seriesRenderer.addXTextLabel(9, "27");
        seriesRenderer.addXTextLabel(10, "30");
        seriesRenderer.addXTextLabel(11, "33");
        seriesRenderer.addXTextLabel(12, "36");
        seriesRenderer.addXTextLabel(13, "39");
        seriesRenderer.addXTextLabel(14, "42");
        seriesRenderer.addXTextLabel(15, "45");
        seriesRenderer.addXTextLabel(16, "48");
        seriesRenderer.addXTextLabel(17, "51");
        seriesRenderer.addXTextLabel(18, "54");
        seriesRenderer.addXTextLabel(19, "57");
        seriesRenderer.addXTextLabel(20, "60");


        seriesRenderer.setXLabelsColor(Color.WHITE);
        seriesRenderer.setLabelsTextSize(20);
        seriesRenderer.setYLabelsColor(0, Color.WHITE);
        seriesRenderer.setPointSize(18f);
        seriesRenderer.setMarginsColor(Color.parseColor("#52C6F7"));
        seriesRenderer.setClickEnabled(false);
        seriesRenderer.setShowGridX(true);
        seriesRenderer.setGridColor(Color.WHITE);

        XYSeriesRenderer xySeriesRenderer1 = new XYSeriesRenderer();
        xySeriesRenderer1.setPointStyle(PointStyle.CIRCLE);
        xySeriesRenderer1.setPointStrokeWidth(3f);
        xySeriesRenderer1.setColor(Color.WHITE);
        xySeriesRenderer1.setFillPoints(true);
        xySeriesRenderer1.setDisplayChartValues(false);
        xySeriesRenderer1.setLineWidth(10.0f);//设置 线的宽度
        seriesRenderer.addSeriesRenderer(xySeriesRenderer1);
        return seriesRenderer;
    }

    public XYMultipleSeriesDataset getDataSet_tmp3() {
        XYMultipleSeriesDataset seriesDataset = new XYMultipleSeriesDataset();
        XYSeries xySeries = new XYSeries("二氧化碳");
        for (int i = 0; i < listco.size(); i++) {
            Log.w("Lxh", "co : " + listco.get(i));
            xySeries.add(i + 1, listco.get(i));
        }
        seriesDataset.addSeries(xySeries);
        return seriesDataset;
    }
}
